let uploadRef;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    uuid: {
      type: String,
      value: null,
    },
    taskList: {
      type: Array,
      value: [],
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    payTypeIndex: 0,
    payTypeList: [{
        type: 101,
        name: '个人付款'
      },
      {
        type: 201,
        name: '汇联易'
      }, {
        type: 301,
        name: '美团'
      }, {
        type: 401,
        name: '公司支付'
      }, {
        type: 501,
        name: '其他'
      }
    ],
    invoiceTypeIndex: 0,
    invoiceTypeList: [{
        type: 101,
        name: '无发票'
      },
      {
        type: 201,
        name: '发票待寄回'
      }, {
        type: 301,
        name: '发票已寄回'
      }
    ],
    payStepIndex: 0,
    payStepList: [{
      type: 101,
      name: '在出差过程中付款'
    }, {
      type: 201,
      name: '在回公司过程中付款'
    }, {
      type: 301,
      name: '在出差出发过程中付款'
    }, {
      type: 401,
      name: '在出差回家过程中付款'
    }, {
      type: 501,
      name: '其他'
    }],
    formData: {
      startStation: '',
      startDatetime: '',
      endStation: '',
      endDatetime: '',
      price: '',
      isTransSeat: 0,
      isEjiakanghu: 0,
      isWeilao: 0,
      payType: 101,
      invoiceType: 101,
      payStep: 101,
      remark: '',
      imageList: [],
      tasks: [],
    }
  },

  attached() {
    uploadRef = this.selectComponent('#uploadRef')
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showStartDatetime: function (e) {
      this.selectComponent("#startDatetime").show();
    },

    changeStartDatetime(e) {
      //选择的结果
      console.log(e);
      this.setData({
        'formData.startDatetime': e.detail.result
      })
    },

    showEndDatetime: function (e) {
      this.selectComponent("#endDatetime").show();
    },
    
    changeEndDatetime(e) {
      //选择的结果
      console.log(e);
      this.setData({
        'formData.endDatetime': e.detail.result
      })
    },

    addNewFee() {
      this.triggerEvent('addNewFee');
    },

    chooseTasks() {
      this.selectComponent('#choose-task').show(this.data.formData.tasks)
    },

    changeTasks(e) {
      this.setData({
        'formData.tasks': e.detail
      })
    },

    deleteTask(e) {
      let tasks = this.data.formData.tasks
      tasks.splice(e.currentTarget.dataset.index, 1)
      this.setData({
        'formData.tasks': tasks
      })
    },

    changePayType(e) {
      let index = parseInt(e.detail.value)
      this.setData({
        payTypeIndex: index,
        'formData.payType': this.data.payTypeList[index].type
      })
    },

    changeInvoiceType(e) {
      let index = parseInt(e.detail.value)
      this.setData({
        invoiceTypeIndex: index,
        'formData.invoiceType': this.data.invoiceTypeList[index].type
      })
    },

    changePayStep(e) {
      let index = parseInt(e.detail.value)
      this.setData({
        payStepIndex: index,
        'formData.payStep': this.data.payStepList[index].type
      })
    },

    //以下为调用upload 方法上传示例
    uploadImg(file) {
      //上传的文件信息
      console.log('组件内返回的文件信息', file)
      // 文件上传的函数，返回一个promise
      //上传成功后返回上传后的图片地址，上传失败则返回false即可
      return new Promise((resolve, reject) => {
        //调用api上传，所有需要参数自行补充【上传自行实现，便于扩展】
        wx.uploadFile({
          //请换成真实的上传接口地址，示例接口已关闭
          url: this.data.serverUrl,
          name: 'file',
          // header: {},
          // formData:{},
          filePath: file.path,
          success: (res) => {
            //上传成功后，将图片地址返回
            //此处逻辑自行根据接口返回判断
            console.log(res)
            if (true) {
              //返回上传成功后的图片地址，根据实际接口返回处理数据 
              const data = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
              let url = data.data.url
              resolve(url)
            } else {
              //上传失败
              reject(false)
            }
          },
          fail: (err) => {
            //上传失败
            console.log(err)
            reject(false)
          }
        })
      })
    },
    upload() {
      if (this.data.imageList.length === 0) {
        wx.showToast({
          title: '选择图片！',
          icon: 'none'
        })
        return;
      }
      //调用upload 方法上传，并传入函数，初始上传时勿传入索引值
      uploadRef && uploadRef.upload(this.uploadImg)
    },

    //重新上传
    reUpload(e) {
      //当前上传按钮索引值
      console.log(e.detail.index)
      //调用upload 方法上传，并传入函数，且此时需要传入index值
      uploadRef && uploadRef.upload(this.uploadImg, e.detail.index)
    },
    complete(e) {
      console.log(e.detail)
      this.data.imageList = e.detail.imgArr
    },

    deleteOption() {
      const vm = this
      wx.showModal({
        showCancel: true,
        cancelText: '取消',
        confirmText: '删除',
        content: '是否确认删除该费用？',
        complete: (res) => {
          if (res.confirm) {
            vm.triggerEvent('delete', this.properties.uuid)
          }
        }
      })
    },

  },

})